<?php

namespace App\Lib;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

class JdSdk
{
    public $jd;
    public function __construct(){
//        $this->jd = new \Jd\Application('57495fee2587e4edf63b12bf92700577', '05a970437feb4cf498bd5c9d5ae0dcae');

    }

//    public function goodsDetail($skuIds){
//        $res = $this->jd->union->open->goods->bigfield->query($skuIds);
//        return $res;
//    }

    public function goodsQuery($page,$perPage,$skuIds){
        $client = new Client([
            'base_uri' => 'https://api.tbk.dingdanxia.com',
        ]);
        $resGet = $client->get("/jd/query_goods",[
            'query' => [
                'apikey' => 'bXtigjh3oyEWhZ3wDLo9GaOM84vcr5yQ',
                'skuIds' => implode(",",$skuIds),
                'pageIndex' => $page,
                'pageSize' => $perPage
            ],
            'verify' => false
        ]);
        return json_decode($resGet->getBody(),true);
    }

    public function goodsDetail($skuIds){

        $client = new Client([
            'base_uri' => 'https://api.tbk.dingdanxia.com',
        ]);
        $resGet = $client->get("/jd/item_detail",[
            'query' => [
                'apikey' => 'bXtigjh3oyEWhZ3wDLo9GaOM84vcr5yQ',
                'skuIds' => implode(",",$skuIds),
            ],
            'verify' => false
        ]);
        return json_decode($resGet->getBody(),true);

    }

    public static function pictureId($pictureUrl){
        $client = new Client();
        $file = fopen($pictureUrl, 'r');

        $headers = [
            'creatorId' => '23319254',
            'monitorSource' => 0,
        ];
        $options = [
            'multipart' => [
                [
                    'name' => 'file',
                    'contents' => $file,
                    'filename' => "",
                ]
            ],
            'verify' => false
            ];
        $request = new Request('POST', 'https://cecoimage.jd.com/content/image', $headers);
        $resStr = $client->sendAsync($request, $options)->wait();
         $res = json_decode($resStr->getBody(),true);
         if(isset($res['code']) && $res['code'] == 0){
             return $res['result']['pictureId'];
         }
         return false;
    }

    public static function upVideo($uploadUrl,$file){
        $client = new Client();

        $options = [
            'multipart' => [
                [
                    'name' => 'file',
                    'contents' => $file,
                    'filename' => "",
                ]
            ],
            'verify' => false
        ];
        $request = new Request('POST', $uploadUrl);
        $resStr = $client->sendAsync($request, $options)->wait();
    }


    public static function webUploadVideo($videoName,$resFile,$cookie){

        $body = [
            "videoName" => $videoName,
            "fileSize" => strlen($resFile),
            "style" => 2,
        ];

        $client = new Client();
        $headers = [
            'Referer' => 'https://dr.jd.com/',
//            'Cookie' => 'thor=D2AF17597AB8966691528D374CA7890E55A6882E8360A7F31DC4848396EE5A43ED9F01911A304245D894476DFBDE5AA6D435689AB3C6B34ED1F5EE9588AD80339806AA13D3B2F4CEA51013DED77A0FD18CEA466D3007AC0AEADC9E138DCA5EE22E3035670A761E012CE49BA38058F55D02D1610137381330BB87FDA6330E42A966ABE3670BEA398FA49656A9C28F9191	',
            'User-Agent' => 'Apifox/1.0.0 (https://apifox.com)'
        ];
        $options = [
            'form_params' => [
                'functionId' => 'videoQueryUploadUrl',
                'appid' => 'dr-platform',
                'client' => 'pc',
                'loginType' => '3',
                'body' => json_encode($body)
            ],
            "verify" => false
        ];
        $request = new Request('POST', 'https://api.m.jd.com/videoQueryUploadUrl', $headers);
        $res = $client->sendAsync($request, $options)->wait();
        return json_decode($res->getBody(),true);
    }
}
